package com.jf.cloud.service.sysmanager.common.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApiParamUtil {
	
	static Logger log = LoggerFactory.getLogger(ApiParamUtil.class);
	/**
	 * 打印当前API 收到的参数
	 * keyWords：关键字
	 * 编写者：ouyang
	 * 创建时间：Aug 31, 2016</pre>
	 */
	@SuppressWarnings("all")
	public static void viewParams(HttpServletRequest request,String keyWords){
		Map<String,String> params = new HashMap<String,String>();
		Map<String,String[]> requestParams = request.getParameterMap();
		try {
			for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
				String name = (String) iter.next();
				String[] values = (String[])requestParams.get(name);
				String valueStr = "";
				for (int i = 0; i < values.length; i++) {
					valueStr = (i == values.length - 1) ? valueStr + values[i] :valueStr + values[i] + ",";
				}
				params.put(name, valueStr);
			}
			log.info((keyWords==null?"":keyWords)+" ★  获得调用参数:" + net.sf.json.JSONObject.fromObject(params).toString() +" ★ queryString=" + request.getQueryString()+"  ★ 来自IP:"+HttpServletUtil.getIp(request)); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void viewParams(HttpServletRequest request){
		viewParams(request, ""); 
	}
}
